 package my.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import af.spring.AfRestError;
import my.db.Authority;
import my.db.Clazz;
import my.db.Student;
import my.db.Teacher;
import my.db.TerCrs;
import my.tool.MybatisApi;

@Controller
public class IndexController {

	@GetMapping("index")
	public String hello(Model model,HttpServletRequest request) {
		
		model.addAttribute("datas",SimpleJS(request));
		
		return "Index";
	}
	
	public static Object SimpleJS(HttpServletRequest request) {
		
		HttpSession session = request.getSession();
		
		Student student = (Student) session.getAttribute("student");
		Teacher teacher = (Teacher) session.getAttribute("teacher");
		JSONObject datas = new JSONObject();
		JSONObject jobj = new JSONObject();
		List leftMenu = null;
		if(student != null) {
			leftMenu =  MybatisApi.selectList("zhy.sesIndex.getStuMenu", Authority.class);
			jobj.put("id", student.getId());
			jobj.put("userId", student.getStudentId());
			jobj.put("clazzId", student.getClazzId());
			jobj.put("realName", student.getRealName());
			jobj.put("sex", student.getSex());
			jobj.put("role", "学生");

		}else if(teacher != null) {
			if(teacher.getIsManager()) {
				leftMenu =  MybatisApi.selectList("zhy.sesIndex.getMgerMenu", Authority.class);
			}else {
				leftMenu =  MybatisApi.selectList("zhy.sesIndex.getTacherMenu", Authority.class);
			}
			jobj.put("id", teacher.getId());
			jobj.put("userId", teacher.getTeacherId());
			jobj.put("realName", teacher.getRealName());
			jobj.put("academyId", teacher.getAcademyId());
			jobj.put("isDean", teacher.getIsDean());
			jobj.put("isManager", teacher.getIsManager());
			jobj.put("sex", teacher.getSex());
			if(teacher.getIsDean()) {
				jobj.put("role", "院系主任");
			}else {
				jobj.put("role", "教师");
			}
			
		}
		datas.put("jobj", jobj);
		datas.put("leftMenu", leftMenu);
		return datas;
	}

}
